package cn.bobohost.crmrbac.service.impl;

import cn.bobohost.crmrbac.dao.mapper.EmployeeRoleMapper;
import cn.bobohost.crmrbac.dao.mapper.RoleMapper;
import cn.bobohost.crmrbac.pojo.po.EmployeeRole;
import cn.bobohost.crmrbac.pojo.po.Role;
import cn.bobohost.crmrbac.pojo.query.QueryObject;
import cn.bobohost.crmrbac.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 波波老师
 * @since 2025-05-21
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMapper roleMapper;

    private final EmployeeRoleMapper employeeRoleMapper;

    @Override
    public PageInfo<Role> selectDepartmentListPage(QueryObject queryObject) {


        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());

        //查询列表
        List<Role> roleList = roleMapper.selectList(null);

        return new PageInfo<Role>(roleList);

    }

    @Override
    public List<Role> findListByEmploeeId(Long id) {

        //查询角色和员工关联表
        QueryWrapper qw =new QueryWrapper();
        qw.eq("employee_id",id);

        List<EmployeeRole> employeeRoleList = employeeRoleMapper.selectList(qw);

        if(null ==employeeRoleList ||employeeRoleList.isEmpty()){
            return null;
        }

        //批量查询角色对象
        List<Role> roleList =new ArrayList<>();

        for (EmployeeRole employeeRole:employeeRoleList){
            Long roleId = employeeRole.getRoleId();
            Role role = roleMapper.selectById(roleId);
            roleList.add(role);
        }

        return roleList;
    }
}
